﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Recherche_Concert
{
    public class Event
    {
        #region Constructeurs

        public Event()
        {

        }

        public Event(string id, string titreEvent, string description, string urlImage, DateTime dateDebut, string urlSite)
        {
            this.id = id;
            this.titreEvent = titreEvent;
            this.description = description;
            this.dateDebut = dateDebut;
            this.urlImage = urlImage;
            this.urlSite = urlSite;
        }


        #endregion

        #region Champs

        private const string URL_GET_EVENT = "http://ws.audioscrobbler.com/2.0/?method=artist.getevents&artist=#Artist#&api_key=27c34d179daebafc88cf103c35a96f67";
        private string id = "inconnu";
        private string titreEvent = "inconnu";
        private string description = "inconnu";
        private DateTime dateDebut;
        private string urlImage = "inconnu";
        private string urlSite = "inconnu";

        #endregion

        #region Propriétés

        public string _URL_GET_EVENT
        {
            get { return URL_GET_EVENT; }
        }

        public string _id
        {
            get { return id; }
            set { id = value; }
        }

        public string _titreEvent
        {
            get { return titreEvent; }
            set { titreEvent = value; }
        }

        public string _description
        {
            get { return description; }
            set { description = value; }
        }

        public DateTime _dateDebut
        {
            get { return dateDebut; }
            set { dateDebut = value; }
        }

        public string _urlImage
        {
            get { return urlImage; }
            set { urlImage = value; }
        }

        public string _urlSite
        {
            get { return urlSite; }
            set { urlSite = value; }
        }

        #endregion

        #region Méthodes

        public static List<Event> getAllEventsByArtist(string artist)
        {
            string urlEvent = URL_GET_EVENT.Replace("#Artist#", artist);
            List<Event> lesEvents = new List<Event>();
            //XElement xEvent = XDocument.Load(URL_GET_EVENT).Root;

            IEnumerable<XElement> xlesEvents = XDocument.Load(urlEvent).Root.Element("events").Elements("event");

            foreach (XElement xEvent in xlesEvents)
            {
                string input = xEvent.Element("description").NextNode.NextNode.NextNode.ToString();
                int deb = input.IndexOf(">") + 1;
                int fin = input.IndexOf("jpg") - 17;
                string sub = input.Substring(deb, fin) ;

                Event unEvent = new Event(xEvent.Element("id").Value,
                                          xEvent.Element("title").Value,
                                          xEvent.Element("description").Value,
                                          sub, //Temporaire
                                          (DateTime)xEvent.Element("startDate"),
                                          xEvent.Element("url").Value);

                lesEvents.Add(unEvent);
            }
            return lesEvents;
        }

        #endregion
    }
}